# include <cstdio>
# include <iostream>
# define MAXN 1005
using namespace std;

int f[MAXN], T, M, t, w;

int main() { 
    freopen("medic.in", "r", stdin);
    freopen("medic.out", "w", stdout);
    scanf("%d %d", &T, &M);
    for(int i = 1; i <= M; i++) { 
        scanf("%d %d", &t, &w);
        for(int j = T; j >= t; j--) { 
            f[j] = max(f[j], f[j - t] + w);
        }
    }
    printf("%d\n", f[T]);
    return 0;
}
